﻿using BarcodeScanner.Mobile;
using CommunityToolkit.Maui;
using CommunityToolkit.Maui.Storage;
using MaBlaApp.Data;
using Microsoft.Extensions.Logging;

namespace MaBlaApp;

public static class MauiProgram
{
    public static MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();
        builder
            .UseMauiApp<App>()
            .UseMauiCommunityToolkit()
            .ConfigureFonts(fonts =>
            {
                fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
            })
            .ConfigureMauiHandlers(handlers =>
            {
                handlers.AddBarcodeScannerHandler();
            });

        builder.Services.AddMauiBlazorWebView();

#if DEBUG
        builder.Services.AddBlazorWebViewDeveloperTools();
        builder.Logging.AddDebug();
#endif

        builder.Services.AddSingleton<WeatherForecastService>();
        builder.Services.AddSingleton<TodoService>();
#if ANDROID
        builder.Services.AddSingleton<BleTester>();
#endif

        builder.Services.AddSingleton<LoginUserManager>();

        //NuGet安装Microsoft.Extensions.Http
        //访问Identity Server 4服务器的HttpClient
        builder.Services.AddHttpClient<Ids4Client>()
            .ConfigureHttpClient(c => c.BaseAddress = new Uri("http://localhost:5000"));
        //Windows调试
        //.ConfigureHttpClient(c => c.BaseAddress = new Uri("http://10.0.2.2:5000"));//安卓模拟器，AndroidManifest.xml要添加android:usesCleartextTraffic="true"支持访问http网站

        //设置软件语言
        LangHelper.SetCurrentLang(LangHelper.CurrentLang);

        //注册文件存储服务
        builder.Services.AddSingleton<IFileSaver>(FileSaver.Default);

        //注册选择文件夹服务
        builder.Services.AddSingleton<IFolderPicker>(FolderPicker.Default);

        return builder.Build();
    }
}
